package dao

import (
	"morleyStoneHome/model"
)

type chainDao struct {
}

var ChainDao = newChainDao()

func newChainDao() *chainDao {
	return &chainDao{}
}

//查池子配置
func (n *chainDao) FindPoolList() (syspool []*model.SysPool, err error) {
	err = Db.Where("1 = 1").Find(&syspool).Error
	if err != nil {
		return nil, err
	}
	return syspool, nil
}

//查每日释放
func (n *chainDao) FindRelease() (sysRelease []*model.SysRelease, err error) {
	err = Db.Where("1 = 1").Find(&sysRelease).Error
	if err != nil {
		return nil, err
	}
	return sysRelease, nil
}

//写每日质押总奖励
func (n *chainDao) InsertSreward(s *model.StoSreward) error {
	sreward, err := n.GetSreward()
	if nil != err {
		return err
	}
	day1 := sreward.Ctime.Day()
	day2 := s.Ctime.Day()
	if day1 == day2 {
		return nil
	} else {
		err = Db.Create(s).Error
		if nil != err {
			return err
		}
		return nil
	}

	//var s1 = &model.StoSreward{}
	////date := util.GetDay(s.Ctime)
	//date := time.Now()
	//s.Ctime = date
	//
	//Db.Create(s)
	//Db.Where("ctime = ? ", date).Find(s1)
	//if s1 == nil || s1.Sreward == 0 {
	//	err := Db.Create(s).Error
	//	if nil != err {
	//		return err
	//	}
	//} else {
	//	fmt.Println(s1)
	//}
	//return nil
}

//查每日质押总奖励
func (n *chainDao) GetSreward() (s *model.StoSreward, err error) {
	err = Db.Last(&s).Error
	//date := util.GetDay(t1)
	//res := Db.Where("ctime = ?", date).Find(&s)
	if err != nil {
		return nil, err
	}
	return s, nil
}

//根据地址、nonce查一笔交易
func (n *chainDao) GetTx(addr string, nonce int) (s *model.StoTx, err error) {
	err = Db.Where("t_account = ? and nonce = ?", addr, nonce).First(&s).Error
	return s, err
}
